#ifndef ClassBaseModulH
 #define ClassBaseModulH

 #include "ModulsAddressSpace.h"
 #include "CPU_drv_I2C.h"



typedef enum
{
  ALL_RIGHT = 0,
  I2C_ERROR,
  NO_ANSWER,
  WRONG_TYPE,
  WRONG_SETTING,
  WAIT_SCAN,
  UNKNOWN_COM,
  LINE_STRETCH
} STATUS_MODUL;

typedef enum
{
 IREQon      = 1<<1,
 WrngMODon   = 1<<2,
 PwrM2on     = 1<<3,
 WrFALSH     = 1<<4,
 PwrDWNon    = 1<<5,
 SCrst       = 1<<6,
 LDIODon     = 1<<7,
 IREQoff     = 1<<9,
 WrngMODoff  = 1<<10,
 PwrM1on     = 1<<11,
 LDIODoff    = 1<<15
} CR1flags;

typedef struct
{
  uint16_t    OutSet : 8;
  uint16_t    OutRst : 8;
} switch_bits;

typedef union
{
  uint16_t         msk;
  switch_bits      msk_bit;
} Switch_Out;



 
 #define DevIDmsk     0x3F
 #define DevIDmin     1
 #define DevIDmax     ( DevIDmsk )

 #define NofChMax     8

/****************************************************************************************************/
/****************************************************************************************************/

 #define MinModTypeBASE      0x00
 #define BASEsize          ( sizeof(BASEmodul) )






class BASEmodul
{
protected:
  STATUS_I2C StatusI2C;
  uint8_t HWver, SWver, NofBytMax, DevID, NofCh;
  uint8_t CR1St, ModKey;
  uint16_t CHmask;
//////////////////////////////////////////////////////////////////////////////////////////////////////  
private:
  STATUS_MODUL CheckStatusI2C(STATUS_I2C state);
  STATUS_MODUL RdDevID(uint8_t devID);
  STATUS_MODUL WrDevID(uint8_t oldID);

//
  
protected:
//
  STATUS_MODUL RdMod(uint16_t address, uint8_t NofBytes);
  STATUS_MODUL WrMod(uint16_t address, uint8_t NofBytes, const void *pSource);
//
  STATUS_MODUL ReadModInfo(uint8_t MinType);
  STATUS_MODUL WrKey(void)                   { return WrMod(aKey,1,&ModKey); }
//
  virtual STATUS_MODUL SetUpModul(void);
  virtual void SetUpModState(void)           {}
  virtual void SetUpNofCH(void);
//
public:
//
  BASEmodul(uint8_t devID, uint8_t modKey);
  virtual ~BASEmodul() {}
//
  virtual STATUS_MODUL WriteStart(uint8_t StartTyp, uint8_t ChNumb)     { return UNKNOWN_COM; }
  virtual bool CheckMeasEnd(portTickType TimeOut_mS)                    { return true; }
  virtual STATUS_MODUL ReadCHvals(void)                                 { return UNKNOWN_COM; }
  virtual STATUS_MODUL SwitchRO(uint16_t ROmask)                        { return UNKNOWN_COM; };
  virtual STATUS_MODUL SwitchDO(uint16_t DOmask)                        { return UNKNOWN_COM; };
  virtual STATUS_MODUL SetUpAO(void)                                    { return UNKNOWN_COM; };
//
  STATUS_I2C Get_StatusI2C(void)             { return StatusI2C; }
  uint8_t Get_HWver(void)                    { return HWver; }
  uint8_t Get_SWver(void)                    { return SWver; }
  uint8_t Get_CR1(void)                      { return CR1St; }
//  
  STATUS_MODUL ScanDevID(uint8_t MaxDevID);
//
  STATUS_MODUL WriteChPwrON(uint16_t ChPwrONmask);
  STATUS_MODUL WriteChPwrOFF(uint16_t ChPwrOFFmask);
  STATUS_MODUL WrCR1(CR1flags cr1);
//
  STATUS_MODUL ReadStatus(void);
//
};
/****************************************************************************************************/
/****************************************************************************************************/
#endif